(function() {

    if (!window.ajax) window.ajax = function(params) {

        if (window.XMLHttpRequest) {
        // Code for IE7+, Firefox, Chrome, Opera, Safari
            var xmlhttp = new XMLHttpRequest();
        } else {
        // Code for IE6, IE5
            var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        this.parseJSON = function(json_string) {
            if (typeof json_string == "string") {
                return window.JSON && window.JSON.parse ? JSON.parse(json_string) : eval('(' + json_string + ')');
            }

            return json_string;
        }

        var method = (/post/i.test(params.method)) ? 'POST' : 'GET'; //default: GET
        var async = (params.async === false) ? false : true;  //default: true
        var url = params.url || window.location.href;
        var data = params.data || '';
        var contentType = (params.contentType === false) ? false : true;  //default: true

        if (method == 'GET' && data != '') {
            url += (/\?/.test(url)) ? '&' : '?';
            url += data;
        }

        // The onreadystatechange event is triggered every time the readyState changes
        xmlhttp.onreadystatechange = function() {
            // The readyState property holds the status of the XMLHttpRequest
            if (xmlhttp.readyState == 4) { // readyState = 4: request finished and response is ready
                if (xmlhttp.status == 200) { // status = 200: "OK"; status = 404: "Page not found"
                    var response = (params.parse_json) ? parseJSON(xmlhttp.responseText) : xmlhttp.responseText; //get the response from a server
                    params.success(response);
                } else {
                    params.error();
                }
            }
        }

        // Send a request to a server
        xmlhttp.open(method, url, async);

        if (method == 'POST') {
            if (contentType) {
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            }

            xmlhttp.send(data);
        } else {
            xmlhttp.send();
        }

    }

})();